package com.project.myPackage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.project.myPackage.common.R;
import com.project.myPackage.entity.Orderdetails;
import com.project.myPackage.entity.Orders;
import com.project.myPackage.service.OrderdetailsService;
import com.project.myPackage.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = "订单管理")
@RestController
@Slf4j
@RequestMapping("/")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderdetailsService orderdetailsService;

    @ApiOperation(value = "管理员获取所有订单信息")
    @RequestMapping("/getAllOrders")
    public R<?> getAllOrders() {
        List<Orders> list = ordersService.list();
        if(list != null){
            return R.success(list,"获取所有订单信息列表成功！");
        }else{
            return R.error("获取所有订单信息列表失败！");
        }
    }


    @ApiOperation(value = "管理员根据订单状态获取订单列表")
    @RequestMapping("/getAllOrdersByStatus")
    public R<?> getAllOrdersByStatus(@RequestParam Integer status) {
        List<Map<String, Object>> list = ordersService.getAllOrdersByStatus(status);
        if(list != null){
            SimpleDateFormat sdfCreate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Map<String, Object> map : list) {
                Date createdAt = (Date) map.get("created_at");
                if (createdAt != null) {
                    map.put("created_at", sdfCreate.format(createdAt));
                }
            }

            //修改时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Map<String, Object> map : list) {
                Date updatedAt = (Date) map.get("updated_at");
                if (updatedAt != null) {
                    map.put("updated_at", sdf.format(updatedAt));
                }
            }
            return R.success(list,"获取所有订单信息列表成功！");
        }else{
            return R.error("获取所有订单信息列表失败！");
        }
    }



    @ApiOperation(value = "根据用户id 订单状态获取")
    @RequestMapping("/getAllOrdersByUserId")
    public R<?> getAllOrdersByUserId(@RequestParam Integer user_id,@RequestParam(required = false) Integer status) {
        List<Map<String, Object>> list = ordersService.getAllOrdersByUserId(user_id, status);
       if(list != null){
           SimpleDateFormat sdfCreate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           for (Map<String, Object> map : list) {
               Date createdAt = (Date) map.get("created_at");
               if (createdAt != null) {
                   map.put("created_at", sdfCreate.format(createdAt));
               }
           }

           //修改时间格式
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           for (Map<String, Object> map : list) {
               Date updatedAt = (Date) map.get("updated_at");
               if (updatedAt != null) {
                   map.put("updated_at", sdf.format(updatedAt));
               }
           }
           return R.success(list,"获取所有订单信息列表成功！");
       }else{
           return R.error("获取所有订单信息列表失败！");
       }
    }

    @ApiOperation(value = "新增订单信息")
    @PostMapping("/addOrders")
    public R<?> addOrders(@RequestBody Orders orders) {

        boolean save = ordersService.save(orders);
        if(save){
            //获取订单id
            return R.success(orders,"新增订单信息成功！");
        }else{
            return R.error("新增订单信息失败！");
        }
       }

       @ApiOperation(value = "删除订单信息")
       @RequestMapping("/deleteOrders")
      public R<?> deleteOrders(Integer order_id) {
           boolean removeById = ordersService.removeById(order_id);
           if(removeById){
               // 删除订单信息成功后删除订单详情信息
               LambdaQueryWrapper<Orderdetails> queryWrapper = new LambdaQueryWrapper<>();
               queryWrapper.eq(Orderdetails::getOrder_id, order_id);

               boolean remove = orderdetailsService.remove(queryWrapper);
               if(!remove){
                   // 删除订单详情信息失败
                   return R.error("删除订单信息失败！");
               }else{
                   // 删除订单详情信息成功
                   return R.success("删除订单信息成功！");
               }
           }else{
               return R.error("删除订单信息失败！");
           }
       }

       @ApiOperation(value = "修改订单信息")
       @PostMapping("/updateOrders")
        public R<?> updateOrders(@RequestBody Orders orders) {
           boolean update = ordersService.updateById(orders);
           if(update){
               return R.success("修改订单信息成功！");
           }else{
               return R.error("修改订单信息失败！");
           }
       }
}
